<?php

namespace app\api\controller;

use app\api\BaseController;
use app\api\middleware\Auth;
use app\api\validate\WorkValidate;
use app\load\loadServices\WorksServices;
use think\Exception;
use think\exception\ValidateException;

/**
 * 日报周报月报表
 */
class Work extends BaseController
{


    /**
     * 免登录方法
     * 控制器中间件 [登录 不需要鉴权]
     * @var array
     */
    protected $middleware = [
        Auth::class => ['except' => []]
    ];



    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $param = input();
        try {

            validate(WorkValidate::class)->scene('list')->check($param);
            $page = [
                'limit' => $param['limit'] ?? 10,
                'page' => $param['page'] ?? 10,
            ];
            $page = array_merge($page, $param);

            $where[] = ['status', '=', 1];

            if (!empty($param['type'])) {
                $where[] = ['type', '=', $param['type']];
            }
            if ($param['is_this'] == 1) {
                $where[] = ['admin_id', '=', JWT_UID];
            } else {
                $where[] = ['type_user', 'like', '%' . JWT_UID . '%'];
            }
            $result = WorksServices::work()->list($where, 'id,type,type_user,works,plans,remark,admin_id,create_time', $page['limit'] ?: 10, $page['page'] ?: 1);
            $this->apiSuccess('请求成功', $result);
        } catch (ValidateException|Exception $e) {
            $this->apiError($e->getMessage());
        }
    }


    /**
     * 详情
     * @return void
     */
    public function info()
    {
        $param = input();
        try {

            validate(WorkValidate::class)->scene('info')->check($param);
            $where[] = ['id', '=', $param['id']];
            $where[] = ['status', '=', 1];
            $result = WorksServices::work()->info($where, 'id,type,type_user,works,plans,remark,admin_id,create_time', $param['is_this']);
            $this->apiSuccess('请求成功', $result);
        } catch (ValidateException|Exception $e) {
            $this->apiError($e->getMessage());
        }
    }


    /**
     * 添加
     * @return void
     * @throws \Exception
     */
    public function save()
    {
        $param = input();
        try {
            validate(WorkValidate::class)->scene('save')->check($param);
            WorksServices::work()->save($param,JWT_UID);
            $this->apiSuccess('保存成功');
        } catch (ValidateException $e) {
            $this->apiError($e->getMessage());
        }
    }

}